#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'Test.php';
require_once 'app/init.php';
require_once 'AirHttpTest.php';
require_once 'classes/TestRecord.php';
require_once 'classes/TestRelatedRecord.php';

// set up the table(s)
AIR2_DBManager::init();
// clean up table from any prior aborted run
$conn = AIR2_DBManager::get_master_connection();
$conn->execute('drop table if exists test_related');
$conn->execute('drop table if exists test');
Doctrine::createTablesFromArray(array('TestRecord', 'TestRelatedRecord'));

// create some dummy data
$uuid = '1a2b3c4d';
$test_record = new TestRecord();
$test_record->test_string = '1234helloworld';
$test_record->test_uuid = $uuid;




//$test_rel = new TestRelatedRecord();
//$test_rel->tr_string_1 = 'mystring';

$test_record['TestRelatedRecord'][]->tr_string_1 = 'mystring0';
$test_record['TestRelatedRecord'][0]->tr_string_2 = 'yourstring0';
$test_record['TestRelatedRecord'][]->tr_string_1 = 'mystring1';
$test_record['TestRelatedRecord'][1]->tr_string_2 = 'yourstring1';
$test_record['TestRelatedRecord'][]->tr_string_1 = 'mystring2';
$test_record['TestRelatedRecord'][2]->tr_string_2 = 'yourstring2';

$test_record->save();


plan(42);

// init browser
$browser = new AirHttpTest();
$browser->set_user('testuser', array());

// check that related aren't accessible through html
$browser->set_content_type(AirHttpTest::$HTML);
$page = $browser->http_get('/test_crud/'.$uuid.'/boggle');
is($browser->resp_code(), 415, "GET html on related record");

// set to json
$browser->set_content_type(AirHttpTest::$JSON);

// check that nonexistent relation returns 404
$page = $browser->http_get('/test_crud/'.$uuid.'/fakeboggle');
is($browser->resp_code(), 404, "GET html on nonexistent related");

/**********************
 * list default related via GET
 */
ok( $resp = $browser->http_get('/test_crud/'.$uuid.'.json'),
    "GET one related crud");
ok( $thing = json_decode($resp,true), "JSON decode GET one");
is( $thing['success'], true, "GET one success");
is( is_array($thing['radix']['TestRelatedRecord']), true, "GET one 'related' is array");
is( count($thing['radix']['TestRelatedRecord']), 3, "GET one - 3 related records");
is( isset($thing['radix']['TestRelatedRecord'][0]['tr_string_1']), true,
    "GET one - columns prefixed with uri_name" );
is( isset($thing['radix']['TestRelatedRecord'][0]['tr_string_1']), true,
    "GET one - columns original name set" );
is( isset($thing['radix']['TestRelatedRecord'][0]['tr_test_id']), false,
    "GET one - blacklist related columns" );


/**********************
 * list related via GET_list
 */
ok( $resp = $browser->http_get('/test_crud/'.$uuid.'/boggle.json'),
    "GET list of related crud");
ok( $thing_list = json_decode($resp,true), "JSON decode GET_list");
is( $thing_list['success'], true, "GET_list success");
is( $thing_list['total'], 3, "GET_list total");
is( isset($thing_list['radix'][0]['tr_test_id']), false, "GET_list - blacklist");


/**********************
 * create related via POST
 */
$newthing = array(
    'tr_string_1' => 'new string 01',
    'tr_string_2' => 'new string 02',
);
ok( $resp = $browser->http_post('/test_crud/'.$uuid.'/boggle.json',
        array('radix' => json_encode($newthing)) ), "POST related new crud");
ok( $thing_post = json_decode($resp,true), "JSON decode POST");
is( $thing_post['success'], true, "POST related success");
is( $thing_post['radix']['tr_string_1'], $newthing['tr_string_1'],
    "POST returns original value");
ok( $thing_post['radix']['tr_uuid'], "POST returns new UUID");
is( isset($thing_post['radix']['tr_test_id']), false, "POST related -- blacklist");
$new_uuid = $thing_post['radix']['tr_uuid'];


/**********************
 * get single related via GET
 */
ok( $resp = $browser->http_get('/test_crud/'.$uuid.'/boggle/'.$new_uuid.'.json')
        , "GET one related" );
ok( $single = json_decode($resp,true), "JSON decode GET one related" );
is( $single['success'], true, "GET one related success" );
is( isset($single['radix']), true, "GET one related single returned" );
is( $single['radix']['tr_string_1'], $newthing['tr_string_1'], "GET one related original value");
is( isset($single['radix']['tr_test_id']), false, "GET one related --- blacklist" );


/**********************
 * update via PUT
 */
ok( $resp = $browser->http_put('/test_crud/'.$uuid.'/boggle/'.$new_uuid.'.json',
    array( 'radix' => json_encode(array('tr_string_2' => 'different')))),
    "change tr_string_2 via PUT related");
ok( $put = json_decode($resp,true), "JSON decode PUT related");
is( $put['success'], true, "PUT related success");
is( $put['radix']['tr_string_2'], 'different', "PUT related changed value");


/**********************
 * test related FK crud
 */
ok( $resp = $browser->http_get('/test_related_crud/'.$new_uuid.'.json'), "GET one related" );
ok( $single = json_decode($resp, true), "JSON decode GET one related" );
is( $single['success'], true, "GET one related success" );
is( isset($single['radix']), true, "GET one related single returned" );

// this test doesn't concern cre/upd stamps, so just unset them for now
unset( $single['radix']['TestRecord']['CreUser'] );
unset( $single['radix']['TestRecord']['UpdUser'] );
unset( $single['radix']['TestRecord']['test_cre_dtim'] );
unset( $single['radix']['TestRecord']['test_upd_dtim'] );
is_deeply( $single['radix']['TestRecord'],
    array (
//        'test_id' => '1', --- note, this is a PK, so it gets blacklisted
        'test_uuid' => '1a2b3c4d',
        'test_string' => '1234helloworld',
    ), "related single loaded with FK");

/**********************
 * DELETE related
 */
// check that it exists
$page = $browser->http_get('/test_crud/'.$uuid.'/boggle/'.$new_uuid.'.json');
is($browser->resp_code(), 200, "related record (to DELETE) exists!");
ok( $resp = $browser->http_delete('/test_crud/'.$uuid.'/boggle/'.$new_uuid.'.json')
        , "DELETE related" );
ok( $delete = json_decode($resp,true), "JSON decode DELETE related" );
is( $delete['success'], true, "DELETE related success" );
is( $delete['radix']['tr_uuid'], $new_uuid, "UUID returned on DELETE related");
// check that it's gone
$page = $browser->http_get('/test_crud/'.$uuid.'/boggle/'.$new_uuid.'.json');
is($browser->resp_code(), 404, "DELETE'd relation now non-existent");


// clean up tables
$conn->execute('drop table test_related');
$conn->execute('drop table test');
?>
